//
//  XManagementViewWalletController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/27.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XManagementViewWalletController.h"
#import "ManagementWalletCell.h"
#import "ImportTheWalletViewController.h"
#import "WalletInfoViewController.h"
@interface XManagementViewWalletController ()
@property (nonatomic,strong) NSMutableArray*data;
@end

@implementation XManagementViewWalletController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = AdaptedWidth(100);
    UIView *footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(50))];
    XThemeFlatButton *btn = [XThemeFlatButton button];
    [footV addSubview:btn];
    btn.frame = CGRectMake(kNormalSpace, AdaptedWidth(10), kScreenWidth-2*kNormalSpace, AdaptedWidth(40));
    [btn setTitle:XLocalized(@"导入钱包") forState:UIControlStateNormal];
    kWeakSelf(self);
    [btn addTapBlock:^(UIButton * _Nonnull btn) {
        ImportTheWalletViewController *vc = [ImportTheWalletViewController new];
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    self.tableView.tableFooterView = footV;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.data = [[XHDataBaseManager shareInstance]selectAllLocalWallet];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.data count];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ManagementWalletCell *cell = [ManagementWalletCell cellForTableView:tableView];
    Wallet *wallet = [self.data objectOrNilAtIndex:indexPath.row];
    cell.titleLab.text = [wallet.wallet_uid isEqualToString:CURRENT_WALLET_ID]?[NSString stringWithFormat:@"%@ （%@）",wallet.wallet_name,XLocalized(@"当前钱包")]:wallet.wallet_name;
    cell.keyLab.text = wallet.wallet_public_key;
    cell.availableLab.text = IsNilOrNull(wallet.balance)?@"0000 EOS":wallet.balance;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Wallet *wallet = [self.data objectOrNilAtIndex:indexPath.row];
    WalletInfoViewController *info = [WalletInfoViewController new];
    info.wallet = wallet;
    [self.navigationController pushViewController:info animated:YES];
}
@end
